<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Admin_Stock extends CI_Controller {

    public function Admin_Stock() {
        parent::__construct();
        $this->load->model('user_model');
        $this->load->model('admin_stock_model');
        $this->load->library('administrator_authenticate');
    }

    /* public function index()
      {
      $this -> administrator_authenticate -> check_login();
      $data['content'] = 'admin_stock_view';
      $data['header'] = 'ស្តុក > ការបញ្ចេញស្តុក';
      $data['jquery_ui'] = '';
      $data['jquery_dialog'] = '';
      $this->load->view('admin_template', $data);
      } */

    public function issue() {
        $this->administrator_authenticate->check_login();

        $data['official_orders'] = $this->_official_order();

        $data['content'] = 'admin_stock_view';
        $data['header'] = 'ស្តុក៖ ការបញ្ចេញស្តុក';
        $data['jquery_ui'] = '';
        $data['jquery_dialog'] = '';
        $this->load->view('admin_template', $data);
    }

    function _select_medal() {
        return $this->db->query('SELECT ID, full_name FROM Medal ORDER BY `order` LIMIT 300')->result_array();
    }

    public function history() {
        $this->administrator_authenticate->check_login();
        $data['content'] = 'admin_stock_history_view';
        $data['header'] = 'ស្តុក៖ ប្រវត្តិបំរែបំរួលស្តុក';
        $data['jquery_ui'] = '';
        $data['jquery_dialog'] = '';
        $data['medals'] = $this->_select_medal();
        $this->load->view('admin_template', $data);
    }

    function _official_order() {
        return $this->db->query('
            SELECT ID, Order_Number FROM Official_Order
                WHERE Official_Order.ID NOT IN(
                SELECT Medalissue_Order.official_order_ID FROM Medalissue_Order
                )
           ORDER BY ID ')->result_array();
    }

    public function add_stock() {
        $this->administrator_authenticate->check_login();
        $this->load->model('medal_stock_in_model');
        $result = $this->medal_stock_in_model->add();
        echo json_encode($result);
    }

    public function stock_list() {
        $this->administrator_authenticate->check_login();
        $this->load->model('medal_stock_in_model');
        $output = $this->medal_stock_in_model->getStockIn();
        echo json_encode($output);
    }

    public function medal_in_stock() {
        $this->administrator_authenticate->check_login();
        $medal = $this->db->query("SELECT Medal.Medal_Class_ID, Medal.Medal_Type_ID, Medal.`level`, Medal_Stock.amount FROM Medal LEFT JOIN Medal_Stock ON Medal_Stock.medal_ID = Medal.ID ");
        $result = $medal->result_array();
        echo json_encode($result);
    }

    public function medal_in_stock_by_id() {
        $id = trim(check_get("id"));
        $medal = $this->db->query("select Medal.Medal_Type_ID, Medal.Medal_Class_ID, Medal.ID, Medal_Stock_In_Amount.amount from Medal_Stock_In
INNER JOIN Medal_Stock_In_Amount ON Medal_Stock_In_Amount.Medal_Stock_In_ID = Medal_Stock_In.ID AND Medal_Stock_In.ID = '$id'
RIGHT OUTER JOIN Medal ON Medal.ID = Medal_Stock_In_Amount.Medal_ID");
        $result = $medal->result_array();
        echo json_encode($result);
    }

    public function stock_issue() {
        $this->administrator_authenticate->check_login();
        $this->load->model('medal_stock_issue_model');
        if (trim(@$_POST['issueid']) != "") {
            $result = $this->medal_stock_issue_model->edit();
        } else {
            $result = $this->medal_stock_issue_model->add();
        }
        echo json_encode($result);
    }

    public function issue_list() {
        $this->administrator_authenticate->check_login();
        $this->load->model('medal_stock_issue_model');
        $output = $this->medal_stock_issue_model->getStockIssue();
        echo json_encode($output);
    }

    public function read_issue() {
        $this->administrator_authenticate->check_login();
        $result = $this->db->get_where("Medal_Stock_Issue", array("ID" => check_post("id")))->result_array();
        $rowOfficialOrderIDs = $this->db->query("
            SELECT Medalissue_Order.official_order_ID AS id, Official_Order.Order_Number AS value FROM Medalissue_Order 
            INNER JOIN Official_Order ON Official_Order.ID = Medalissue_Order.official_order_ID
            WHERE Medalissue_Order.medal_stock_issue_ID ='" . check_post("id") . "' ")->result_array();
        $result[0]['official_order_id'] = $rowOfficialOrderIDs;
        echo json_encode($result[0]);
    }

    public function read_official() {
        $this->administrator_authenticate->check_login();
        $medal = $this->db->query("
            SELECT Medal_Stock_Issue_Amount.Medal_Stock_Issue_ID, Medal.Medal_Class_ID, Medal.Medal_Type_ID, Medal_Stock_Issue_Amount.amount FROM Medal_Stock_Issue_Amount
                RIGHT JOIN Medal ON Medal.ID = Medal_Stock_Issue_Amount.Medal_ID
                WHERE Medal_Stock_Issue_Amount.Medal_Stock_Issue_ID ='" . check_post('issue_id') . "'");
        $result = $medal->result_array();
        echo json_encode($result);
    }

    public function getMedalOrder() {
        $this->administrator_authenticate->check_login();
        $issues = trim($_POST['issue_id']) != "" ? $_POST['issue_id'] : "''";
        if (trim($issues) != "") {
            $query = $this->db->query("
                    SELECT Medal.ID, Medal.Medal_Type_ID, Medal.Medal_Class_ID, SUM(Official_Medal_Order.amount) AS possible_amount FROM Official_Order
                        INNER JOIN Official_Medal_Order ON Official_Medal_Order.Official_Order_ID = Official_Order.ID AND Official_Order.ID IN ($issues)
                        RIGHT JOIN Medal ON Medal.ID = Official_Medal_Order.Medal_ID
                        GROUP BY Medal.ID, Medal.Medal_Class_ID, Medal.Medal_Type_ID
                        ORDER BY Medal.Medal_Type_ID, Medal.Medal_Class_ID  ")->result_array();
            echo json_encode($query);
        }
    }

    public function getAvailableOfficerOrder() {
        $this->administrator_authenticate->check_login();
        $officialOrders = $this->_official_order();
        foreach ($officialOrders as $officialOrder) {
            echo "<option value='{$officialOrder['ID']}'>{$officialOrder['Order_Number']}</option>";
        }
    }

    public function printPreview($id = NULL) {
        $this->administrator_authenticate->check_login();
        $baseurl = site_url('images');
//        $this->load->library('mpdf/mpdf');
//        $mpdf = new mPDF('c');
        $data['root'] = $this->config->item('base_url');
        if (!empty($id)) {
            $data['ids'] = explode("-", $id);
        }
        $html = $this->load->view('/pdf/print_stock_issue', $data, true);
//        $mpdf->WriteHTML($html);
        $data['content'] = $html;
        $this->load->view('print_layout', $data);
//        $mpdf->Output();
    }


    public function printSummary($startDate, $endDate, $year = "") {
        $this->administrator_authenticate->check_login();
         $data['medal'] = $this->db->query("
            SELECT Medal.ID, Medal.Medal_Class_ID, Medal.Medal_Type_ID, SUM(Medal_Stock_Issue_Amount.amount) amount FROM Medal_Stock_Issue 
                    INNER JOIN Medal_Stock_Issue_Amount
                    ON Medal_Stock_Issue.ID = Medal_Stock_Issue_Amount.Medal_Stock_Issue_ID AND Medal_Stock_Issue.date BETWEEN '$startDate' AND '$endDate'
                    RIGHT JOIN Medal ON Medal.ID = Medal_Stock_Issue_Amount.Medal_ID
                    GROUP BY Medal.ID, Medal.Medal_Class_ID, Medal.Medal_Type_ID ORDER BY Medal.Medal_Type_ID, Medal.Medal_Class_ID
            ")->result_array();

        $baseurl = site_url('images');
        $data['root'] = $this->config->item('base_url');
        if (trim($year) != "") {
            $data['filter'] = "សំរាប់ឆ្នាំ {$year}";
        }else{            
            $startDate = date("d/m/Y", strtotime($startDate));
            $endDate = date("d/m/Y", strtotime($endDate));
            $data['filter'] = "សំរាប់រយៈពេល៖ ពីថ្ងៃទី {$startDate} ដល់ {$endDate} ";
        }
        $html = $this->load->view('/pdf/print_summary', $data, true);
        $data['content'] = $html;
        $this->load->view('print_layout', $data);
    }

}